package com.tuangou.tuangoucomsumer.comsumer.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.tuangou.tuangoucomsumer.comsumer.dao.CityDao;
import com.tuangou.tuangoucomsumer.comsumer.vo.UserAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tuangou.tuangoucomsumer.comsumer.dao.AddressDao;
import com.tuangou.tuangoucomsumer.comsumer.entity.AddressEntity;
import com.tuangou.tuangoucomsumer.comsumer.service.AddressService;


@Service("addressService")
public class AddressServiceImpl extends ServiceImpl<AddressDao, AddressEntity> implements AddressService {
    @Autowired
    private AddressDao addressDao;
    @Autowired
    private CityDao cityDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AddressEntity> page = this.page(
                new Query<AddressEntity>().getPage(params),
                new QueryWrapper<AddressEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<UserAddress> findList() {
        ArrayList<UserAddress> list = new ArrayList<>();

        List<AddressEntity> addressEntities = addressDao.selectList(null);
        for (AddressEntity entity : addressEntities) {
            String sum = "";
            Integer provinceId = entity.getProvinceId();
            Integer cityId = entity.getCityId();
            Integer countryId = entity.getCountryId();
            //查询省市县
            String name = cityDao.findId(provinceId);
            String name1 = cityDao.findId(cityId);
            String name2 = cityDao.findId(countryId);
            sum=sum.concat(name).concat(name1).concat(name2);
            //赋值
            UserAddress userAddress = new UserAddress();
            userAddress.setId(entity.getId());
            userAddress.setUserId(entity.getUserId());
            userAddress.setReceiverName(entity.getReceiverName());
            userAddress.setPhone(entity.getPhone());
            userAddress.setCountAddress(sum);
            userAddress.setReceiverAddress(entity.getReceiverAddress());
            userAddress.setIsDefault(entity.getIsDefault());
            //存入
            System.out.println(userAddress);
            list.add(userAddress);
        }
        return list;
    }

}